package com.qms.repast.utils;

import android.support.annotation.NonNull;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author 泅渡者
 * @date 2018/9/25 0025
 * @describe:
 */
public class ThreadManager {
	/**
	 * 通过ThreadPoolExecutor的代理类来对线程池的管理
	 */
	private static ThreadPollProxy mThreadPollProxy;


	/**
	 * 单列对象
	 *
	 * @return
	 */
	public static ThreadPollProxy getThreadPollProxy () {
		synchronized (ThreadPollProxy.class) {
			if (mThreadPollProxy == null) {
				mThreadPollProxy = new ThreadPollProxy(3, 6, 1000);
			}
		}
		return mThreadPollProxy;
	}

	/**
	 * 通过ThreadPoolExecutor的代理类来对线程池的管理
	 */
	public static class ThreadPollProxy {
		/**
		 * 线程池执行者 ，java内部通过该api实现对线程池管理
		 */
		private ThreadPoolExecutor poolExecutor;
		private int corePoolSize;
		private int maximumPoolSize;
		private long keepAliveTime;
		private final AtomicInteger threadNumber = new AtomicInteger(1);
		private final String namePrefix = "Repast";

		public ThreadPollProxy (int corePoolSize, int maximumPoolSize, long keepAliveTime) {
			this.corePoolSize = corePoolSize;
			this.maximumPoolSize = maximumPoolSize;
			this.keepAliveTime = keepAliveTime;
		}

		/**
		 * 对外提供一个执行任务的方法
		 *
		 * @param r
		 */
		public void execute (Runnable r) {
			if (poolExecutor == null || poolExecutor.isShutdown()) {
				poolExecutor = new ThreadPoolExecutor(
						//核心线程数量
						corePoolSize,
						//最大线程数量
						maximumPoolSize,
						//当线程空闲时，保持活跃的时间
						keepAliveTime,
						//时间单元 ，毫秒级
						TimeUnit.MILLISECONDS,
						//线程任务队列
						new LinkedBlockingQueue<Runnable>(),
						//创建线程的工厂
						new ThreadFactory() {
							@Override
							public Thread newThread (@NonNull Runnable runnable) {
								return new Thread(runnable, namePrefix + threadNumber.getAndIncrement());
							}
						}
				);
			}
			poolExecutor.execute(r);
		}
	}
}
